<?php
class NUploader {
function &getInstance ($options=array('path'=>'','prefix'=>'neo'))

    // this implements the 'singleton' design pattern.

    {

        static $instances;
	
        $signature = serialize($options);
        if (!isset($instances[$signature])) {

            $c = __CLASS__;

            $instances[$signature] = new $c($options['path'],$options['prefix']);

        } // if

        

        return $instances[$signature];

        

    }
function NUploader($path="",$prefix="neo",$allowtype=array()){

umask(0);
$this->ext2mime = include (dirname(__FILE__)."/mimetype.inc.php");
$this->prefix=$prefix;
$this->allowtype=$allowtype;
if (!$path) {die (" not set path");}
$this->path=$path;

}
function _checkallowmime($mimetype){
if (!count ($this->allowtype)) return true;
foreach ($this->allowtype as $type){
if ($this->ext2mime['type']==$mimetype) return true;
	
}
// TODO error
return false;
}
function FetchMedia($name,$filename=''){
if (!is_array($_FILES[$name]['name'])){
	
	IF (!$_FILES[$name]['name']) return false;
$source=$_FILES[$name]['tmp_name'];
$mimetype = $_FILES[$name]['type'];
$this->_checkallowmime($mimetype);
$origFileName = $_FILES[$name]['name'];
$ext = substr($origFileName,strrpos($origFileName,'.')+1);

if (!$filename){
$filename = uniqid($this->prefix).'.'.$ext;
} else {
$filename = $filename.'.'.$ext;

}
$destination=$this->path."/".$filename;
if (!move_uploaded_file($source,$destination)){
echo "Cant move file"; return false;
};
return $this->filename = $filename;

} else {

foreach ($_FILES[$name]['name'] as $key=>$value){ //foreach begin
$source=$_FILES[$name]['tmp_name'][$key];
$mimetype = $_FILES[$name]['type'][$key];
$origFileName = $_FILES[$name]['name'][$key];
$ext = substr($origFileName,strrpos($origFileName,'.')+1);

if (!$filename){
$filename_tmp = uniqid($this->prefix).'.'.$ext;
} else{
$filename_tmp=$filename.$key.'.'.$ext;}
$destination=$this->path."/".$filename_tmp;

if (!move_uploaded_file($source,$destination)){
echo "Cant move file"; return false;
};
return $this->filename[] = $filename_tmp;

}	//foreach end
}

}

function GetFileName(){
return $this->filename;
}

}
?>